package neusoft.edu.cn.a215lmeordering.activities;

import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.squareup.picasso.Picasso;

import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

import neusoft.edu.cn.a215lmeordering.Listener.ListListener;
import neusoft.edu.cn.a215lmeordering.Listener.Listener;
import neusoft.edu.cn.a215lmeordering.R;
import neusoft.edu.cn.a215lmeordering.adapter.ContentAdapter;
import neusoft.edu.cn.a215lmeordering.bean.AddCart;
import neusoft.edu.cn.a215lmeordering.bean.Content;
import neusoft.edu.cn.a215lmeordering.bean.FoodDetails;
import neusoft.edu.cn.a215lmeordering.bean.IsCollect;
import neusoft.edu.cn.a215lmeordering.model.AddCartModel;
import neusoft.edu.cn.a215lmeordering.model.CollectionFoodModel;
import neusoft.edu.cn.a215lmeordering.model.ContentModel;
import neusoft.edu.cn.a215lmeordering.model.FoodDetailsModel;
import neusoft.edu.cn.a215lmeordering.model.IsCollectModel;
import neusoft.edu.cn.a215lmeordering.model.ShopfoodListModel;

public class FoodDetailsActivity extends AppCompatActivity {

    private FoodDetails foodDetails;
    private List<Content> list;
    private AddCart addCart;
    private IsCollect isCollect;
    private int food_id;
    private ImageView imageView,food_collect;
    private TextView name,price,jianjie;
    private int flag=1;
    private int shuaxin_flag=0;
    private ContentAdapter contentAdapter;
    private RecyclerView contentRecycler;
    SwipeRefreshLayout swipeRefreshLayout;
    private Button buy;
    private int num=-1;
    private EditText num_editText;

    private Listener listener=new Listener() {
        @Override
        public void onResponse(Object o) {
            foodDetails = (FoodDetails) o;
            Picasso.get()
                .load("http://172.24.10.175:8080/foodService"+foodDetails.getPic())
                .into(imageView);
            name.setText(foodDetails.getFoodname());
            name.setTextSize(18);
            price.setText("单价："+foodDetails.getPrice());
            jianjie.setText("简介："+foodDetails.getIntro());
        }

        @Override
        public void onFail(String msg) {
            Toast.makeText(getApplicationContext(), "失败："+msg, Toast.LENGTH_SHORT).show();
        }
    };

    private Listener AddCartlistener = new Listener() {
        @Override
        public void onResponse(Object o) {
            addCart = (AddCart) o;
            if(addCart.getSuccess().equals("1")){
                Toast.makeText(FoodDetailsActivity.this, "添加成功", Toast.LENGTH_SHORT).show();
            }else{
                Toast.makeText(FoodDetailsActivity.this, "添加失败", Toast.LENGTH_SHORT).show();
            }

        }

        @Override
        public void onFail(String msg) {

        }
    };

    private ListListener contentListener = new ListListener() {
        @Override
        public void onResponse(List List) {
            list=List;
            contentAdapter.setDate(list);
            if(shuaxin_flag==1){
                shuaxin_flag=0;
                swipeRefreshLayout.setRefreshing(false);
            }
        }

        @Override
        public void onFail(String msg) {

        }
    };

    private Listener foodCollectListener = new Listener() {
        @Override
        public void onResponse(Object o) {
            isCollect = (IsCollect) o;
            System.out.println("iscollect:"+isCollect.getCollected());
            if(isCollect.getCollected().equals("1")){
                Picasso.get().load(R.drawable.food_collect_32).into(food_collect);
            }else{
                Picasso.get().load(R.drawable.food_collect_no_32).into(food_collect);
            }
        }

        @Override
        public void onFail(String msg) {

        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ActionBar actionBar = getSupportActionBar();//去掉标题栏
        actionBar.hide();//去掉标题栏
        setContentView(R.layout.activity_food_details);

        food_id=getIntent().getIntExtra("food_id",0);

        init();
        initRecycler();
        IsCollection();

        FoodDetailsModel foodDetailsModel = new FoodDetailsModel();
        foodDetailsModel.getResultList(food_id,listener);

        ContentModel contentModel = new ContentModel();
        contentModel.getResultList(food_id,contentListener);

        food_collect.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                CollectionFoodModel collectionFoodModel = new CollectionFoodModel(view);
                collectionFoodModel.getResultList(Integer.parseInt(LoginActivity.UserId),food_id);
                IsCollection();
            }
        });

        buy.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(!num_editText.getText().toString().equals("")){
                    num = Integer.parseInt(num_editText.getText().toString());
                    AddCartModel addCartModel = new AddCartModel();
                    addCartModel.getResultList(Integer.parseInt(LoginActivity.UserId), food_id, num, AddCartlistener);
                }else Toast.makeText(FoodDetailsActivity.this, "请输入购买数量...", Toast.LENGTH_SHORT).show();
            }
        });

    }

    private void initRecycler() {
        contentAdapter = new ContentAdapter();

        contentRecycler = findViewById(R.id.contentList);

        //布局管理器  可以设置显示数据的方式为垂直或者水平
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getApplicationContext());
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);

        //每个item如果是确定高度，设置此项提高性能
        contentRecycler.setHasFixedSize(true);

        swipeRefreshLayout = findViewById(R.id.swipeRefresh);

        swipeRefreshLayout.setColorSchemeColors(getResources().getColor(R.color.colorPrimary),
                getResources().getColor(R.color.colorAccent),
                getResources().getColor(R.color.colorPrimaryDark));

        swipeRefreshLayout.setProgressViewOffset(true,50,60);

        swipeRefreshLayout.setRefreshing(false);

        swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                shuaxin_flag=1;
                ContentModel contentModel = new ContentModel();
                contentModel.getResultList(food_id,contentListener);
            }
        });

        contentRecycler.setLayoutManager(linearLayoutManager);
        contentRecycler.setAdapter(contentAdapter);

    }

    private void init() {
        imageView=findViewById(R.id.food_imageView);
        name=findViewById(R.id.food_name);
        price=findViewById(R.id.food_price);
        jianjie=findViewById(R.id.food_jianjie);
        food_collect=findViewById(R.id.food_collect);
        buy=findViewById(R.id.buy);
        num_editText=findViewById(R.id.num);
    }
    private void IsCollection() {
        IsCollectModel isCollectModel = new IsCollectModel();
        isCollectModel.getResultList(Integer.parseInt(LoginActivity.UserId),food_id,flag,foodCollectListener);
    }
}
